/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package towerdefense.model;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import towerdefense.data.BuildTile;
import towerdefense.data.Tile;
import towerdefense.data.WayTile;

/**
 *
 * @author Flo
 */
public class Map extends javax.swing.JPanel {

    private Tile[][] map;
    private int x, y, tilesize;
    private int startX, startY;

    /**
     * Creates new form Map
     */
    public Map(int x, int y, int tilesize) throws Exception {
        initComponents();
        this.setSize(x * tilesize, y * tilesize);
        this.setPreferredSize(new Dimension(x * 50, y * 50));
        this.setMinimumSize(new Dimension(x * 50, y * 50));
        this.y = y;
        this.x = x;
        this.tilesize = tilesize;
        map = new Tile[x][y];
        System.out.println("test constructor before setBackground");
        this.setBackground(Color.red);
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                map[i][j] = new BuildTile();
            }
        }
        createTestPath();
//      createTestTowerTiles();
    }

    private void createTestPath() throws Exception {
        if (x != 10 || y != 10) {
            System.out.println("");
            return;
        }
        map[5][0] = new WayTile(null, 5, 0);
        map[5][1] = new WayTile(null, 5, 1);
        map[5][2] = new WayTile(null, 5, 2);
        map[5][3] = new WayTile(null, 5, 3);
        map[5][4] = new WayTile(null, 5, 4);
        map[5][5] = new WayTile(null, 5, 5);
        map[5][6] = new WayTile(null, 5, 6);
        map[5][7] = new WayTile(null, 5, 7);
        map[5][8] = new WayTile(null, 5, 8);
        map[5][9] = new WayTile(null, 5, 9);
    }

//    private void createTestTowerTiles() throws Exception{
//        map[2][4]=new BuildTile();
//        map[7][4]=new BuildTile();
//    }
    public void set(int x, int y, Tile tile) {
        map[x][y] = tile;
    }

    public Tile get(int x, int y) {
        return map[x][y];
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        drawField(g);
    }

    public void drawField(Graphics g) {
        System.out.println("drawField started");

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                System.out.println("before drawing");
                g.drawImage(map[i][j].getTileimg(), i * 40, j * 40, this);
                //g.setColor(Color.green);
                //g.drawRect(i*tilesize, j*tilesize, tilesize, tilesize);
                System.out.println("after drawing");
            }
        }
        System.out.println("after loop");
        System.out.println("field drawn");
    }

    public int getStartX() {
        return startX;
    }

    public int getStartY() {
        return startY;
    }
}
